//
//  AVKit+Rx.swift
//  NGTC_iOS
//
//  Created by Mr.wang on 2019/1/2.
//  Copyright © 2019 Mr.wang. All rights reserved.
//

import AVFoundation
import RxSwift
import RxCocoa

extension Reactive where Base == AVCaptureDevice {
    
    var torchMode: Observable<AVCaptureDevice.TorchMode> {
        return base.rx.observe(AVCaptureDevice.TorchMode.self, "torchMode")
            .map{ $0 ?? .off }
    }
}

extension Reactive where Base: AVPlayerItem {
    
    var playbackBufferEmpty: Observable<Bool> {
       return base.rx.observe(Bool.self, "playbackBufferEmpty")
            .map{ $0 ?? true }
    }
    
    var playbackLikelyToKeepUp: Observable<Bool> {
        return base.rx.observe(Bool.self, "playbackLikelyToKeepUp")
            .map{ $0 ?? false }
    }
    
    var isreadyToPlay: Observable<Bool> {
        return Observable.combineLatest(
            base.rx.playbackBufferEmpty,
            base.rx.playbackLikelyToKeepUp
            ).map { (empty: $0, keepUp: $1) }
            .map { (status) -> Bool in
                return (!status.empty && status.keepUp)
            }
    }
}
